<template>
  <div :style="{backgroundImage: 'url(' + imgUrl + ')' }" class="ucan-ticket   " :class="{'ticketListImg':imgUrl}" @click="handleClick">
    <div class="ucan-ticket-name">
      {{type}}<span class="ucan-ticket-user-name">
        | {{name}}
      </span>
    </div>
    <div class="ucan-ticket-message">
      <p>{{activeTimeStr}}</p>
      <p>地址: {{address || '-'}}</p>
    </div>
    <div v-if="!cloneOperationBtn&&!groupBuying">
      <div class="ucan-ticket-footer">
        <!-- zhuansongFun()&& -->
        <!-- 云栖大会先去掉 -->
        <!-- <co-button v-if="!transferTicketId&&source==4&&isActive==1&&!disabled&&state===0" class="ucan-ticket-action" color="outline" @click.stop="goForwardPage(activityTitle,type,activityId, id,ticketTypeId,isPay,imgUrl)">
          转赠
        </co-button> -->
        <co-button v-if="isActive&&!disabled" class="ucan-ticket-action" color="primary" @click.stop="openTokenDialog">
          凭证
        </co-button>
        <co-button v-if="isActive&&!disabled" class="ucan-ticket-action" color="primary" @click.stop="meetingShare(ticketTypeId)">
          分享
        </co-button>
      </div>
    </div>
    <div class="ucan-ticket-disabled" v-if="disabled">
      <div class="ucan-ticket-status cenCen">{{disabledText}}</div>
    </div>
    <co-dialog :open="open" class="ticket-token-dialog-wrapper" dialog-class="ticket-token-dialog">
      <div class="ticket-token-dialog-close" @click="open=false">
        <co-icon-close />
      </div>
      <div @click="getCode()" class="ticket-token-dialog-title">
        凭证
      </div>
      <div @click="getCode()" class="ticket-token-dialog-qrcode">
        <img v-if="qrcode" :src="qrcode" />
      </div>
      <div @click="getCode()" class="ticket-token-dialog-desc">
        二维码实时更新，截屏无效
      </div>
    </co-dialog>
    <div v-if="transferTicketId&&source==4&&isActive==1&&isDel==0" class="screen-fixed-box cenCen">
      <div @click="returnGivingTicket(activityId, id,ticketTypeId,isPay)" class="return-saveBtn cenCen">
        撤回转赠
      </div>
    </div>

  </div>
</template>
<script>
import jrQrcode from "jr-qrcode";
import services from "../../api/mine";
import { addZero } from "../../utils";
import ddApi from "./ddApi";
import dd from "dingtalk-jsapi";

export default {
  name: "UcanTicket",
  props: {
    imgUrl: String,
    state: Number, //0 个人票 1团购票
    activityTitle: String,
    isDel: [String, Number], //是否作废
    source: [String, Number], //来源   3赠票4售票5转赠票
    transferTicketId: [String, Number],
    isPay: String,
    groupBuying: Boolean,
    isActive: [String, Number], //是否激活
    id: [String, Number],
    activityId: [String, Number],
    ticketTypeId: [String, Number],
    type: String,
    name: String,
    uTel: [String, Number],
    address: String,
    price: [String, Number],
    activeTime: {
      type: Object,
      default: () => ({
        stime: new Date().getTime(),
        etime: new Date().getTime(),
        activeDate: ""
      })
    },
    cloneOperationBtn: Boolean //是否关闭操作的按钮
  },
  computed: {
    // <!--  state 0：个人 1：团体  5转赠票 -->
    // 门票状态
    disabled() {
      console.log("transferTicketId", this.transferTicketId);
      console.log("isActive", this.isActive);

      if (
        this.transferTicketId &&
        (this.isActive == 1 || this.isActive == 5) &&
        this.source == 4
      ) {
        return true;
      } else {
        return false;
      }
    },
    disabledText() {
      // transferTicketId门票中这个不为空 并且isActive=1就是说明在转赠中 ，如果isActive=5说明转赠成功
      if (this.transferTicketId) {
        if (this.isActive == 1) {
          return "转赠中";
        } else if (this.isActive == 5) {
          return "已转赠";
        }
      }
      return "";
    },
    activeTimeStr() {
      let { stime, etime, activeDate } = this.activeTime;
      const start = new Date(stime || undefined);
      const end = new Date(etime || undefined);

      activeDate = this.$yunQiTicketTime(this.ticketTypeId) || activeDate;
      //
      if (stime == etime) {
        // return `${addZero(start.getMonth() + 1)}月${addZero(
        //   start.getDate()
        // )}日 ${activeDate}`;
        return `${this.$moment(
          this.$yunQiTicketDataStime(Number(this.ticketTypeId)) || stime
        )
          .utcOffset(480)
          .format("MM月DD日")} ${activeDate}`;
      } else {
        // return `${addZero(start.getMonth() + 1)}月${addZero(
        //   start.getDate()
        // )}日~${addZero(end.getMonth() + 1)}月${addZero(
        //   end.getDate()
        // )}日 ${activeDate}`;
        return `${this.$moment(
          this.$yunQiTicketDataStime(Number(this.ticketTypeId)) || stime
        )
          .utcOffset(480)
          .format("MM月DD日")}~${this.$moment(
          this.$yunQiTicketDataEtime(Number(this.ticketTypeId)) || etime
        )
          .utcOffset(480)
          .format("MM月DD日")} ${activeDate}`;
      }
    },
    deadline() {
      if (!this.deadlineTime) return "";
      const date = new Date(this.deadlineTime);
      return `${addZero(date.getMonth() + 1)}月${addZero(date.getDate())}日`;
    }
  },
  data() {
    return {
      open: false,
      qrcode: "",
      deadlineTime: null
    };
  },
  methods: {
    zhuansongFun() {
      let zhuansongList = [
        "15972961091",
        "13758141708",
        "15868154582",
        "13127882837",
        "18621532154",
        "17623878198",
        "15855431282",
        "1760130268995005"
      ];
      return zhuansongList.indexOf(sessionStorage.getItem("zhuansong")) != -1
        ? true
        : false;
    },
    //列表展示需要用到查看详情
    handleClick(e) {
      this.$emit("click", e);
    },
    // 撤回转赠票
    async returnGivingTicket(activityId, ticketId, ticketTypeId, isPay) {
      const confirm = await this.$confirm(`您确定进行转赠撤回么？`, {
        title: "温馨提示",
        okLabel: "确认",
        cancelLabel: "取消",
        showCancel: true
      });
      if (!confirm) {
        return;
      }
      const { success } = await services.returnGivingTicket({
        activityId,
        ticketId,
        ticketTypeId,
        isPay
      });
      if (success) {
        this.$toast("撤回成功");
        this.$parent.getTicket();
      }
    },
    // 转赠
    goForwardPage(
      activityTitle,
      tTypeName,
      activityId,
      ticketId,
      ticketTypeId,
      isPay,
      backgroundImage
    ) {
      this.$router.push({
        path: "/forward/give",
        query: {
          activityTitle,
          tTypeName,
          activityId,
          ticketId,
          ticketTypeId,
          isPay,
          backgroundImage
        }
      });
    },
    // 实时更新
    getCodeTime() {
      setTimeout(() => {
        // if (this.open) {
        //   console.log("重新");
        //   this.getCode();
        //   this.getCodeTime();
        // }
        this.open = false;
      }, 25000);
    },
    // 展示凭证
    async openTokenDialog() {
      await ddApi.setScreenBrightnessAndKeepOn();
      this.open = true;
      this.getCode();
      this.getCodeTime();
    },
    // 分享
    meetingShare(ticketTypeId) {
      dd.biz.util.share({
        'type':0,
        'url':`https://page.dingtalk.com/wow/dingtalk/act/meetingshare${ticketTypeId}?wh_biz=tm`,
        'title':'钉钉2020新品发布会 我已抢票成功',
        'content':'阿里钉钉2020新品发布暨生态大会， 购票享智能礼，席位有限，点我抢占先机！',
        'image':'https://gw.alicdn.com/tfs/TB187Xcrhz1gK0jSZSgXXavwpXa-300-300.png'
      })
    },

    // 获取code
    getCode() {
      console.log("请求");
      let subData = {
        activityId: this.activityId
      };
      // 云栖大会
      if (this.activityId == 33) {
        subData.uName = this.name || "";
        subData.uTel = this.uTel || "";
      }

      services.getQRCode(subData).then(({ qrcode }) => {
        console.log("qrcode", qrcode);
        if (!qrcode) {
          this.$toast("暂无数据");
          return;
        }
        this.qrcode = jrQrcode.getQrBase64(qrcode, {
          width: 356,
          height: 356
        });
        this.$forceUpdate();
      });
    }
  }
};
</script>
<style lang="less">
.ticket-logo {
  position: absolute;
  left: 0px;
  top: 0px;
  width: 180px;
  height: 100%;
  // border: 1px solid red;
}
.ticket-logo > img {
  width: 90%;
  height: auto;
}
.ucan-ticket {
  width: 100%;
  height: auto;
  min-height: 160px;
  background: rgba(0, 0, 0, 0.5);
  border: 1px solid rgba(61, 61, 61, 0.8);
  padding: 24px 32px 0;
  // padding-left: 200px;
  position: relative;
  z-index: 1;
  text-align: left;
  color: #fff;
  .ucan-ticket-logo {
    width: 200px;
    margin-bottom: 32px;
    margin-left: 8px;
  }
  + .ucan-ticket {
    margin-top: 20px;
  }

  .ucan-ticket-bg {
    position: absolute;
    left: 0;
    right: 0;
    top: 0;
    bottom: 0;
    width: 100%;
    height: 100%;
    z-index: -1;
  }

  .ucan-ticket-name {
    font-size: 44px;
    padding-left: 8px;
  }

  .ucan-ticket-user-name {
    font-size: 30px;
  }

  .ucan-ticket-message {
    font-size: 20px;
    margin-top: 16px;
    padding-bottom: 32px;
    padding-left: 8px;
    > p {
      margin-top: 8px;
      &:first-child {
        margin-top: 0;
      }
    }
  }

  .ucan-ticket-footer {
    display: flex;
    justify-content: space-between;
    align-items: center;
    font-size: 20px;
    color: fade(#eee, 70%);
    padding-top: 28px;
    padding-bottom: 24px;
    border-top: 1px dotted #979797;
    &.only-action {
      justify-content: center;
    }
  }

  .ucan-ticket-action {
    width: 280px;
    height: 72px;
    font-size: 30px;
  }

  .ucan-ticket-disabled {
    position: absolute;
    left: 0;
    right: 0;
    top: 0;
    bottom: 0;
    background-color: rgba(0, 0, 0, 0.2);
  }

  .ucan-ticket-status {
    background: url(https://img.alicdn.com/tfs/TB153TVTAPoK1RjSZKbXXX1IXXa-286-286.png)
      no-repeat;
    background-size: 100%;
    width: 143px;
    height: 143px;
    text-align: center;
    font-size: 28px;
    color: #1a1b1f;
    float: right;
    margin: 26px 26px 0px 0px;
  }
}

.ticket-token-dialog-wrapper {
  padding: 0 32px;
}

.ticket-token-dialog {
  width: 100%;
  border-radius: 4px;
  height: 760px;
  position: relative;
  .co-dialog-body {
    padding-top: 68px;
  }
}

.ticket-token-dialog-title {
  font-size: 34px;
  font-weight: bold;
  color: #333;
  text-align: center;
  margin-bottom: 52px;
}

.ticket-token-dialog-qrcode {
  width: 356px;
  height: 356px;
  margin: 0 auto;
  > img {
    width: 100%;
    height: 100%;
    display: block;
  }
}

.ticket-token-dialog-desc {
  text-align: center;
  font-size: 30px;
  color: #333;
  margin-top: 60px;
}

.ticket-token-dialog-close {
  background: #d8d8d8;
  width: 80px;
  height: 80px;
  border-radius: 50%;
  display: flex;
  justify-content: center;
  align-items: center;
  font-size: 30px;
  color: #35383f;
  position: absolute;
  bottom: -120px;
  left: 50%;
  margin-left: -40px;
}
.ticketListImg {
  padding-top: 80px;
  // background: url("https://gw.alicdn.com/tfs/TB1IdL3bgFY.1VjSZFqXXadbXXa-1080-620.png")
  background-repeat: no-repeat;
  background-size: 100% auto;
}
.return-saveBtn {
  /* position: absolute;
  left: 0px;
  bottom: 0px; */
  width: 100%;
  height: 110px;
  background-image: linear-gradient(-116deg, #3cb9ff 0%, #6044d4 100%);
  /* font-family: PingFangSC-Regular; */
  font-size: 36px;
  color: #eeeeee;
  text-align: center;
  cursor: pointer;
}
</style>

